//
//  Leave.m
//  wheel
//
//  Created by mac on 15-4-1.
//  Copyright (c) 2015年 mac. All rights reserved.
//

#import "Leave.h"
#import "YXbutton.h"

@interface Leave ()

@property (weak, nonatomic) IBOutlet UIImageView *rotateView;

@property (nonatomic, weak) UIButton *currentBtn;
- (IBAction)startAnimation:(UIButton *)sender;

@end

@implementation Leave

+ (instancetype)initWithNib
{
    return [[[NSBundle mainBundle] loadNibNamed:@"wheel" owner:nil options:nil] lastObject];
}

// 不能在initwithcoder中使用连线（插座），因为系统在initwithcoder方法中系统还没准备好连线（插座）

- (void)awakeFromNib
{
    [super awakeFromNib];
    for (int i = 0; i < 12; i ++) {
        YXbutton *leaveBtn = [YXbutton buttonWithType:UIButtonTypeCustom];
        leaveBtn.frame = CGRectMake(0, 0, 66, 143);
        // leaveBtn.backgroundColor = [UIColor redColor];
        [leaveBtn setBackgroundImage:[UIImage imageNamed:@"LuckyRototeSelected"] forState:UIControlStateSelected];
        [leaveBtn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        leaveBtn.layer.position = CGPointMake(self.center.x, self.center.y);
        leaveBtn.layer.anchorPoint = CGPointMake(0.5, 1);
        CGFloat angle = ((i * 30) / 180.0) * M_PI;
        // CGFloat angle = i * (M_PI / 60); 不可以
        leaveBtn.transform = CGAffineTransformMakeRotation(angle);
        
        // 1.设置剪切图片
        // 1.1 加载要剪切的图片
        UIImage *image1 = [UIImage imageNamed:@"LuckyAstrology"];
        UIImage *image2 = [UIImage imageNamed:@"LuckyAstrologyPressed"];
        // 1.2 设置要剪切范围
        CGFloat scal = [UIScreen mainScreen].scale;
        CGFloat smallW = image1.size.width/12 *scal ;
        CGFloat smallH = image1.size.height *scal;
        CGFloat smallX = i * smallW;
        CGFloat smallY = 0;
        CGRect smallRect = CGRectMake(smallX, smallY, smallW, smallH);
        // 1.3 剪切
        CGImageRef images = CGImageCreateWithImageInRect(image1.CGImage, smallRect);
        CGImageRef images2 = CGImageCreateWithImageInRect(image2.CGImage, smallRect);
        // 1.4 把图片设置给btn
        [leaveBtn setImage:[UIImage imageWithCGImage:images] forState:UIControlStateNormal];
         [leaveBtn setImage:[UIImage imageWithCGImage:images2] forState:UIControlStateSelected];
      
        [self.rotateView addSubview:leaveBtn];
    }
    
}

- (void)btnClick:(UIButton *)btn
{
    self.currentBtn.selected = NO;
    btn.selected = YES;
    self.currentBtn = btn;
}


- (IBAction)startAnimation:(UIButton *)sender {
    
    // 创建核心动画
    CA
    // 添加核心动画
}
@end
